//
//  GenearchRegisterViewController.m
//  XDD
//
//  Created by mao on 12/4/14.
//  Copyright (c) 2014 yuhaya. All rights reserved.
//

#import "GenearchRegisterViewController.h"
#import "Tool.h"
#import "User.h"

@interface GenearchRegisterViewController ()

@end

@implementation GenearchRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    CGRect frame = [[UIScreen mainScreen] applicationFrame];
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 55, frame.size.width, frame.size.height) style:UITableViewStyleGrouped];
//    self.table.backgroundColor = [UIColor colorWithRed:0 green:0.5 blue:1 alpha:1];
    self.table.dataSource = self;
    self.table.delegate = self;
    [self.view addSubview:self.table];
   
    [self.view bringSubviewToFront:self.head_layer];
    self.photo.layer.cornerRadius = self.photo.frame.size.width/2;
    self.photo.layer.shouldRasterize = YES;
    self.photo.layer.masksToBounds = YES;
    [self.photo.layer setBorderWidth:3];
    [self.photo.layer setBorderColor:[Tool colorWithHexString:@"#ffffff"].CGColor];
    [self.view bringSubviewToFront:self.photo];
    
    CGRect textFrame = CGRectMake(frame.size.width - 220, 5, 200, 40);
    UIFont * font = [UIFont systemFontOfSize:13];
    
    self.name = [[UITextField alloc] initWithFrame:textFrame];
    self.name.returnKeyType = UIReturnKeyDone;
    self.name.font = font;
    self.name.delegate = self;
    self.name.placeholder = @"输入用户名";
    self.name.textAlignment = NSTextAlignmentRight;
    
    self.full_name = [[UITextField alloc] initWithFrame:textFrame];
    self.full_name.returnKeyType = UIReturnKeyDone;
    self.full_name.font = font;
    self.full_name.delegate = self;
    self.full_name.placeholder = @"输入真实姓名";
    self.full_name.textAlignment = NSTextAlignmentRight;
    
    self.phone = [[UITextField alloc] initWithFrame:textFrame];
    self.phone.returnKeyType = UIReturnKeyDone;
    self.phone.font = font;
    self.phone.delegate = self;
    self.phone.placeholder = @"输入手机号";
    self.phone.textAlignment = NSTextAlignmentRight;
    
    self.card = [[UITextField alloc] initWithFrame:textFrame];
    self.card.returnKeyType = UIReturnKeyDone;
    self.card.font = font;
    self.card.delegate = self;
    self.card.placeholder = @"输入身份证号";
    self.card.textAlignment = NSTextAlignmentRight;
    
    
    self.pwd = [[UITextField alloc] initWithFrame:textFrame];
    self.pwd.returnKeyType = UIReturnKeyDone;
    self.pwd.font = font;
    self.pwd.delegate = self;
    self.pwd.placeholder = @"输入密码";
    self.pwd.textAlignment = NSTextAlignmentRight;
    
    self.sex = [[DCRoundSwitch alloc] initWithFrame:CGRectMake(frame.size.width - 100, 10, 80, 30)];
    [self.sex setOnText:@"男"];
    [self.sex setOffText: @"女"];
   
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
        return YES;
}


-(void) viewWillAppear:(BOOL)animated {
    //注册键盘出现通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardShow:)
                                                 name: UIKeyboardDidShowNotification object:nil];
    //注册键盘隐藏通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardHide:)
                                                 name: UIKeyboardDidHideNotification object:nil];
    [super viewWillAppear:animated];
}

-(void) viewWillDisappear:(BOOL)animated {
    //解除键盘出现通知
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name: UIKeyboardDidShowNotification object:nil];
    //解除键盘隐藏通知
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name: UIKeyboardDidHideNotification object:nil];
    
    [super viewWillDisappear:animated];
}

//自定义键盘打开时触发的事件
-(void) keyboardShow: (NSNotification *)notif {
    
    [UIView animateWithDuration:0.3f animations:^{
        CGRect curFrame=self.table.frame;
        curFrame.origin.y = self.table.frame.origin.y - self.height;
        self.table.frame=curFrame;
    }];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.height = textField.tag * 10;
}

//自定义键盘关闭时触发的事件
-(void) keyboardHide: (NSNotification *)notif {
    [UIView animateWithDuration:0.3f animations:^{
        CGRect curFrame=self.table.frame;
        CGRect status = [[UIApplication sharedApplication] statusBarFrame];
        curFrame.origin.y = self.navigationController.navigationBar.frame.size.height + status.size.height;
        self.table.frame=curFrame;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return 6;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, 50)];
    label.font = [UIFont systemFontOfSize:14];
    NSString * label_name;
    
    switch (indexPath.row) {
        case 0:
            label_name = @"用户名";
            self.name.tag = indexPath.row;
            [cell.contentView addSubview:self.name];
            break;
            
        case 1:
            label_name = @"身份证号";
            self.card.tag = indexPath.row;
            [cell.contentView addSubview:self.card];
            break;
            
        case 2:
            label_name = @"手机号";
            self.phone.tag = indexPath.row;
            [cell.contentView addSubview:self.phone];
            break;
            
        case 3:
            label_name = @"密码";
            self.pwd.tag = indexPath.row;
            [cell.contentView addSubview:self.pwd];
            break;
            
        case 4:
            label_name = @"真实姓名";
            self.full_name.tag = indexPath.row;
            [cell.contentView addSubview:self.full_name];
            break;
            
        case 5:
            label_name = @"性别";
            self.sex.tag = indexPath.row;
            [cell.contentView addSubview:self.sex];
            break;
            
        default:
            break;
    }
    label.text = label_name;
    [cell.contentView addSubview:label];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return NO;
}
- (IBAction)backBtn:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)registerBtn:(UIButton *)sender {
        if([self.phone.text isEqualToString:@""]){
            [Tool showMessage:@"请输入手机号!"];
            return;
        }
    
        NSMutableDictionary * paramter = [NSMutableDictionary dictionary];
        paramter[@"phone"] = self.phone.text;
        paramter[@"par_user_id"] = @"";
        paramter[@"username"] = self.name.text;
        paramter[@"password"] = self.pwd.text;
        paramter[@"id_card"] = self.card.text;
        paramter[@"gender"] = [NSString stringWithFormat:@"%d",self.sex.on ? 0 : 1];
        paramter[@"name"] = self.full_name.text;
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
        NSString * url_stus_get = [NSString stringWithFormat:@"%@?token=%@",URL_USER_ADD,@""];
        
        [manager POST:url_stus_get parameters:paramter success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSDictionary * redata = responseObject;
//            NSArray * data = redata[@"data"];
            NSString * code = redata[@"code"];
            
            if(code.intValue  == 0){
                
                [Tool showMessage:@"注册成功!"];
                [self.navigationController popViewControllerAnimated:YES];
                
            }else{
                [Tool showMessage:redata[@"msg"]];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [Tool showMessage:[error localizedDescription]];
        }];
}
@end